//
//  XMRegisterViewController.m
//  gpos_app
//
//  Created by apple on 2018/8/14.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "XMRegisterViewController.h"
#import "Masonry.h"
#import "XMWJMmViewController.h"
#import "XMZhuCeViewController.h"
#import "MainViewController.h"
#import <sys/utsname.h>
#import "EBCountDownButton.h"
#import "GPosAPI.h"
#import "SVProgressHUD.h"
#import "WKProgressHUD.h"
@interface XMRegisterViewController ()<UITextFieldDelegate>
@property (nonatomic,strong)UIImageView * bjImage;
@property (nonatomic,strong)UIImageView * sjImage;
@property (nonatomic,strong)UIImageView * logoImage;
@property (nonatomic,strong)UITextField * zhanghaoText;
@property (nonatomic,strong)UIButton * detButton;
@property (nonatomic,strong)UIView * zhanghaoView;
@property (nonatomic,strong)UIView * mimaView;
@property (nonatomic,strong)UIImageView * mimaImage;
@property (nonatomic,strong)UIButton * mimaButton;
@property (nonatomic,strong)UITextField * mimaText;
@property (nonatomic,strong)EBCountDownButton * yanzhengButton;
@property (nonatomic,strong)UIButton * registerButton;
@property (nonatomic,strong)UILabel * zhuceLabel;
@property (nonatomic,strong)UIButton * zhuceButton;
@property (nonatomic,strong)UIButton * duanxinButton;
@property (nonatomic,strong)NSDictionary *para;
//@property (nonatomic,strong)SVProgressHUD *hud;
@property (nonatomic,strong)WKProgressHUD *husd;

@end

@implementation XMRegisterViewController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.hidden=YES;
     [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    
}
-(void)showMySmallVIews{
    
    self.husd=[WKProgressHUD popMessage:@"您的账号已在其它设备登录,请重新登录!" inView:self.view duration:3 animated:YES];
  
}


-(void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showMySmallVIews) name:@"NewLoginInView" object:nil];
    self.view.backgroundColor = [UIColor whiteColor];
    
    //隐藏返回提示文字
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:nil];
    [self LoadImageView];
    [self zhanghaoViewLoad];
    [self yanzhengLoad];
    [self zhuceLoad];
    
    NSString * uname = [[NSUserDefaults standardUserDefaults] objectForKey:@"phoneNumber"];
    if (uname != NULL) {
        self.zhanghaoText.text = uname;
    }
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(walkVCClick:) name:@"buttonLoseRespons" object:nil];
   
}



- (void)walkVCClick:(NSNotification *)noti

{
    NSDictionary * dic = [noti userInfo];
    NSString * str = [dic objectForKey:@"info"];
    self.zhanghaoText.text = str;
    self.mimaText.text = @"";
    NSLog(@"就是你你你");
    
}


- (void)LoadImageView {
    self.bjImage = [[UIImageView alloc] init];
    [self.view addSubview:self.bjImage];
    self.logoImage = [[UIImageView alloc] init];
    [self.bjImage addSubview:self.logoImage];
    
    if (IS_IPHONE5) {
        
        [self.bjImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view).with.offset(0);
            make.top.equalTo(self.view).with.offset(0);
            make.right.equalTo(self.view).with.offset(0);
            make.height.equalTo(@269);
        }];
        
        
        [self.logoImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view).with.offset(90);
            //        make.right.equalTo(self.view).with.offset(0);
            make .centerX.equalTo(self.view);
            //            make.height.equalTo(@80);
            //        make.width.lessThanOrEqualTo(@80);
        }];
        
        
    } else {
        
        [self.bjImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view).with.offset(0);
            make.top.equalTo(self.view).with.offset(0);
            make.right.equalTo(self.view).with.offset(0);
            //        make.height.lessThanOrEqualTo(@316);
        }];
        
        
        [self.logoImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view).with.offset(90);
            //        make.right.equalTo(self.view).with.offset(0);
            make .centerX.equalTo(self.view);
            //        make.height.lessThanOrEqualTo(@80);
            //        make.width.lessThanOrEqualTo(@80);
        }];
        
    }
    [self.bjImage setImage:[UIImage imageNamed:@"beijing_dl"]];
    [self.logoImage setImage:[UIImage imageNamed:@"icon_logo"]];
}

- (void)zhanghaoViewLoad {
    
    self.sjImage = [[UIImageView alloc] init];
    [self.view addSubview:self.sjImage];
    
    self.zhanghaoText = [[UITextField alloc] init];
    self.zhanghaoText.keyboardType=UIKeyboardTypeNumberPad;
    [self.view addSubview:self.zhanghaoText];
    
    self.detButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:self.detButton];
    
    self.zhanghaoView = [[UIView alloc] init];
    self.zhanghaoView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:self.zhanghaoView];
    
    self.mimaView = [[UIView alloc] init];
    self.mimaView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:self.mimaView];
    
    self.mimaImage = [[UIImageView alloc] init];
    [self.view addSubview:self.mimaImage];
    
    self.mimaText = [[UITextField alloc] init];
    [self.view addSubview:self.mimaText];
    
    self.mimaButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:self.mimaButton];
    self.yanzhengButton = [EBCountDownButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:self.yanzhengButton];
    
    self.duanxinButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:self.duanxinButton];
    
    
    [self.sjImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).with.offset(40);
        make.top.equalTo(self.bjImage.mas_bottom).with.offset(14);
        //        make.height.lessThanOrEqualTo(@20);
        //        make.width.lessThanOrEqualTo(@20);
        
    }];
    [self.sjImage setImage:[UIImage imageNamed:@"icon_shouji"]];
    
    
    [self.detButton mas_makeConstraints:^(MASConstraintMaker *make) {
        //        make.left.equalTo(self.zhanghaoText).with.offset(24);
        make.centerY.equalTo(self.sjImage);
        make.height.lessThanOrEqualTo(@18);
        make.width.lessThanOrEqualTo(@18);
        make.right.equalTo(self.view).with.offset(-40);
    }];
    [self.detButton setImage:[UIImage imageNamed:@"icon_shutdown"] forState:UIControlStateNormal];
    [self.detButton addTarget:self action:@selector(detbuttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    
    [self.zhanghaoText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.sjImage).with.offset(24);
        make.centerY.equalTo(self.sjImage);
        make.right.equalTo(self.detButton).with.offset(-20);
        
    }];
    self.zhanghaoText.placeholder =@"请输入账号";
    self.zhanghaoText.font = [UIFont systemFontOfSize:14.f];
    self.zhanghaoText.delegate = self;
    
    
    
    [self.zhanghaoView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.view).with.offset(40);
        make.top.equalTo(self.sjImage.mas_bottom).with.offset(12);
        make.right.equalTo(self.view).with.offset(-40);
        make.height.equalTo(@0.5);
        
    }];
    
    
    [self.mimaView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.view).with.offset(40);
        make.top.equalTo(self.zhanghaoView.mas_bottom).with.offset(62);
        make.right.equalTo(self.view).with.offset(-40);
        make.height.equalTo(@0.5);
        
    }];
    
    
    [self.mimaImage mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.view.mas_left).with.offset(40);
        make.bottom.equalTo(self.mimaView).with.offset(-12);
        //        make.height.lessThanOrEqualTo(@20);
        //        make.width.lessThanOrEqualTo(@20);
        
    }];
    [self.mimaImage setImage:[UIImage imageNamed:@"icon_mima"]];
    
    
    [self.mimaButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self.mimaImage);
        //        make.height.equalTo(@18);
        make.width.equalTo(@80);
        //        make.left.equalTo(self.mimaText.mas_right).with.offset(10);
        make.right.equalTo(self.view).with.offset(-40);
    }];
    [self.mimaButton setTitle:@"忘记密码 ?" forState:UIControlStateNormal];
    self.mimaButton.titleLabel.font = [UIFont systemFontOfSize: 14.0];
    [self.mimaButton setTitleColor:[UIColor blueColor]forState:UIControlStateNormal];
    [self.mimaButton addTarget:self action:@selector(mimabuttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.yanzhengButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self.mimaImage);
        //        make.height.equalTo(@18);
        make.width.equalTo(@80);
        //        make.left.equalTo(self.mimaText.mas_right).with.offset(10);
        make.right.equalTo(self.view).with.offset(-40);
    }];
    
    [self.yanzhengButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    self.yanzhengButton.titleLabel.font = [UIFont systemFontOfSize: 14.0];
    [self.yanzhengButton setTitleColor:[UIColor blueColor]forState:UIControlStateNormal];
    [self.yanzhengButton addTarget:self action:@selector(yanzhengbuttonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.yanzhengButton.hidden = YES;
    
    
    
    [self.mimaText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mimaImage).with.offset(24);
        make.centerY.equalTo(self.mimaImage);
        make.right.equalTo(self.mimaButton).with.offset(-20);
        
    }];
    self.mimaText.placeholder =@"请输入密码";
    self.mimaText.font = [UIFont systemFontOfSize:14.f];
    self.mimaText.secureTextEntry = YES;
    self.mimaText.delegate = self;
    
    [self.duanxinButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).with.offset(-40);
        make.top.equalTo(self.mimaImage.mas_bottom).with.offset(15);
        make.width.equalTo(@120);
    }];
    [self.duanxinButton setTitle:@"短信验证码登录" forState:UIControlStateNormal];
    self.duanxinButton.titleLabel.font = [UIFont systemFontOfSize: 15.0];
    self.duanxinButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [self.duanxinButton setTitleColor:[UIColor blueColor]forState:UIControlStateNormal];
    [self.duanxinButton addTarget:self action:@selector(duanxinButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
}

- (void)yanzhengLoad {
    
    self.registerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:self.registerButton];
    
    [self.registerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.mimaView.mas_bottom).with.offset(40);
        make.left.equalTo(self.view).with.offset(40);
        make.right.equalTo(self.view).with.offset(-40);
        //        make.height.equalTo(@60);
        
    }];
    //    self.registerButton.layer.cornerRadius=20;
    [self.registerButton setTitle:@"登录" forState:UIControlStateNormal];
    //    [self.registerButton setBackgroundColor:[UIColor redColor]];
    [self.registerButton setBackgroundImage:[UIImage imageNamed:@"icon_anniu_c"] forState:UIControlStateNormal];
    [self.registerButton addTarget:self action:@selector(registerButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)zhuceLoad {
    
    self.zhuceLabel = [[UILabel alloc] init];
    [self.view addSubview:self.zhuceLabel];
    
    self.zhuceButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:self.zhuceButton];
    
    [self.zhuceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).with.offset(-kSystemNavHeight);
        make.left.equalTo(self.view).with.offset(40);
        make.right.equalTo(self.view).with.offset(-self.view.bounds.size.width/2);
        
    }];
    
    self.zhuceLabel.text = @"还没账户 ?";
    self.zhuceLabel.font = [UIFont systemFontOfSize:15.f];
    self.zhuceLabel.textAlignment= NSTextAlignmentRight;
    
    
    
    [self.zhuceButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.zhuceLabel);
        make.left.equalTo(self.zhuceLabel.mas_right).with.offset(10);
        
    }];
    [self.zhuceButton setTitle:@"立即注册" forState:UIControlStateNormal];
    self.zhuceButton.titleLabel.font = [UIFont systemFontOfSize: 15.0];
    [self.zhuceButton setTitleColor:[UIColor blueColor]forState:UIControlStateNormal];
    [self.zhuceButton addTarget:self action:@selector(zhuceButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
}

- (void)detbuttonAction:(UIButton *)sender{
    
    self.zhanghaoText.text =@"";
    
}


//短信验证的按钮
- (void)duanxinButtonAction:(UIButton *)sender {
    
    if ([self.duanxinButton.titleLabel.text isEqualToString:@"短信验证码登录"]) {
        self.mimaText.placeholder =@"请输入短信验证码";
        [self.duanxinButton setTitle:@"账号密码登录" forState:UIControlStateNormal];
        self.yanzhengButton.hidden = NO;
        self.mimaButton.hidden = YES;
        
    } else {
        self.mimaText.placeholder =@"请输入密码";
        [self.duanxinButton setTitle:@"短信验证码登录" forState:UIControlStateNormal];
        self.yanzhengButton.hidden = YES;
        self.mimaButton.hidden = NO;
        
    }
    
}

//忘记密码
- (void)mimabuttonAction:(UIButton *)sender {
    
    
    UIStoryboard * story = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    
    XMWJMmViewController * WJMmVC = [story instantiateViewControllerWithIdentifier:@"XMWJMmViewController"];
    if (self.zhanghaoText.text.length == 11) {
        WJMmVC.phonedh = self.zhanghaoText.text;
    }
    [self.navigationController pushViewController:WJMmVC animated:YES];
    
    
}

//验证码
- (void)yanzhengbuttonAction:(EBCountDownButton *)sender {
    
    if (self.zhanghaoText.text.length == 0) {
        self.husd=[WKProgressHUD popMessage:@"手机号不能为空" inView:self.view duration:1 animated:YES];
        return;

    }
    if (self.zhanghaoText.text.length != 11) {
        self.husd=[WKProgressHUD popMessage:@"请输入正确的手机号" inView:self.view duration:1 animated:YES];
        return;
    }
    
    [self.yanzhengButton countDownWithDuration:180 completion:^(BOOL finished) {
        NSLog(@"finished");
    }];
    
    NSDictionary * para = @{@"phone":self.zhanghaoText.text,
                            @"type": @(5)
                            };
    
    [GPosAPI requestSendCodeWithParams:para andCallback:^(id obj) {
        NSLog(@"-denglu-%@",obj);
        if ( obj == nil )
        {
            //            self.registerButton.userInteractionEnabled=YES;
            [SVProgressHUD showErrorWithStatus:@"请求失败"];
            [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
            [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
            
        }else{
            
            if ([[obj objectForKey:@"code"] intValue] == 200) {
                
                 self.husd=[WKProgressHUD popMessage:@"发送短信验证码成功" inView:self.view duration:1 animated:YES];
               
            }else {
                
                 self.husd=[WKProgressHUD popMessage:[obj objectForKey:@"message"] inView:self.view duration:1 animated:YES];
               
                
            }

        }
        
    }];
    
}

//登录
- (void)registerButtonAction:(UIButton *)sender {
    
    if ([self.zhanghaoText.text isEqualToString:@""] && [self.mimaText.text isEqualToString:@""]) {
       
         self.husd=[WKProgressHUD popMessage:@"请输入账号" inView:self.view duration:1 animated:YES];
        
        
    }else if (![self.zhanghaoText.text isEqualToString:@""] && ![self.mimaText.text isEqualToString:@""]) {
        
        if ([self.mimaText.placeholder isEqualToString:@"请输入密码"]) {
            NSLog(@"账号登录");
            //账号登录
            if (self.zhanghaoText.text.length != 11) {
                self.husd=[WKProgressHUD popMessage:@"请输入11位账号" inView:self.view duration:1 animated:YES];
                return;
            }
            if (self.mimaText.text.length < 8 && self.mimaText.text.length > 15) {
                self.husd=[WKProgressHUD popMessage:@"请输入18~15个字符" inView:self.view duration:1 animated:YES];
                return;
            }
            self.para=@{@"phone" : self.zhanghaoText.text,
                        @"password" : self.mimaText.text
                        };
            
            [SVProgressHUD showWithStatus:@"正在登陆"];
            [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
            [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
            [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
            self.registerButton.userInteractionEnabled=NO;
            [GPosAPI requestLogInWithParams:self.para andCallback:^(id obj) {
                NSLog(@"---%@",[obj objectForKey:@"message"]);
                if ( obj == nil )
                {
                    self.registerButton.userInteractionEnabled=YES;
                    self.husd=[WKProgressHUD popMessage:@"请求失败" inView:self.view duration:1 animated:YES];
                    
                }else{
                    if ([[obj objectForKey:@"code"] integerValue] == 200)
                    {
                        
                        NSLog(@"-----%@",[obj objectForKey:@"data"]);
                        
                        NSDictionary *s1=[obj objectForKey:@"data"];
                        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"userId"];
                        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"user_token"];
                        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"status"];
                        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"nickName"];
                        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"portrait"];
                        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"phoneNumber"];
                        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"passWord"];
                        
                        
                        
                        [[NSUserDefaults standardUserDefaults] setObject:[s1 objectForKey:@"userId"] forKey:@"userId"];
                        [[NSUserDefaults standardUserDefaults] setObject:[s1 objectForKey:@"token"] forKey:@"user_token"];
                        [[NSUserDefaults standardUserDefaults] setObject:[s1 objectForKey:@"status"] forKey:@"status"];
                        [[NSUserDefaults standardUserDefaults] setObject:[s1 objectForKey:@"nickName"] forKey:@"nickName"];
                        [[NSUserDefaults standardUserDefaults] setObject:[s1 objectForKey:@"portrait"] forKey:@"portrait"];
                        [[NSUserDefaults standardUserDefaults] setObject:self.zhanghaoText.text forKey:@"phoneNumber"];
                        [[NSUserDefaults standardUserDefaults] setObject:self.mimaText.text forKey:@"passWord"];
                        [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"openId"];
                        //                        [[NSUserDefaults standardUserDefaults] synchronize];
                        //OTC_openId
                        [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"OTC_openId"];
                        
                        
                        MainViewController * MainVC = [[MainViewController alloc] init];
                        [self presentViewController:MainVC animated:NO completion:nil];
                        self.registerButton.userInteractionEnabled=YES;
                        
                        [SVProgressHUD dismiss];
                        
                        
                    }else{
                          [SVProgressHUD dismiss];
                         self.husd=[WKProgressHUD popMessage:[obj objectForKey:@"message"] inView:self.view duration:1 animated:YES];
                        self.registerButton.userInteractionEnabled=YES;
                        
                    }
                }
                
//                NSLog(@"=====%@",obj);
            }];
            
        }else {
            
            NSLog(@"验证码登录");
            //验证码登录
            if (self.zhanghaoText.text.length != 11) {
                 self.husd=[WKProgressHUD popMessage:@"请输入正确手机号" inView:self.view duration:1 animated:YES];
                return;
            }
            if (self.mimaText.text.length != 6) {
                self.husd=[WKProgressHUD popMessage:@"请输入正确验证码" inView:self.view duration:1 animated:YES];
                return;
            }
            self.para=@{@"phone" : self.zhanghaoText.text,
                        @"verifyCode" : self.mimaText.text
                        };
            [SVProgressHUD showWithStatus:@"正在登录"];
            [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
            [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
            self.registerButton.userInteractionEnabled=NO;
            [GPosAPI requestverifyLogInWithParams:self.para andCallback:^(id obj) {
                NSLog(@"obj--%@",obj);
                if ( obj == nil )
                {
                    self.registerButton.userInteractionEnabled=YES;
                    [SVProgressHUD dismiss];
                    self.husd=[WKProgressHUD popMessage:@"请求失败" inView:self.view duration:1.5 animated:YES];
                   
                }else{
                    if ([[obj objectForKey:@"code"] intValue] == 200)
                    {
                        NSDictionary *s1=[obj objectForKey:@"data"];
//                        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"userId"];
//                        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"user_token"];
//                        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"status"];
//                        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"nickName"];
//                        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"portrait"];
//                        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"phoneNumber"];
//                        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"passWord"];
                        
//                        NSLog(@"%ld",[[[NSUserDefaults standardUserDefaults] objectForKey:@"userId"] integerValue]);
                        NSString * user = [NSString stringWithFormat:@"%@",[s1 objectForKey:@"userId"]];
                        NSString * sta = [NSString stringWithFormat:@"%@",[s1 objectForKey:@"status"]];
                        
                        [[NSUserDefaults standardUserDefaults] setObject:user forKey:@"userId"];
                        [[NSUserDefaults standardUserDefaults] setObject:[s1 objectForKey:@"token"] forKey:@"user_token"];
                        [[NSUserDefaults standardUserDefaults] setObject:[s1 objectForKey:@"nickName"] forKey:@"nickName"];
                        [[NSUserDefaults standardUserDefaults] setObject:[s1 objectForKey:@"portrait"] forKey:@"portrait"];
                        [[NSUserDefaults standardUserDefaults] setObject:self.zhanghaoText.text forKey:@"phoneNumber"];
                        [[NSUserDefaults standardUserDefaults] setObject:sta forKey:@"status"];
                        [[NSUserDefaults standardUserDefaults] setObject:self.mimaText.text forKey:@"passWord"];
                        [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"openId"];
                        [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"OTC_openId"];
//                        NSLog(@"%ld",[[[NSUserDefaults standardUserDefaults] objectForKey:@"userId"] integerValue]);
                        
                        //                        [[NSUserDefaults standardUserDefaults] synchronize];
                        
                        MainViewController * MainVC = [[MainViewController alloc] init];
                        [self presentViewController:MainVC animated:NO completion:nil];
                        self.registerButton.userInteractionEnabled=YES;
                        
                        [SVProgressHUD dismiss];
                        //                        [SVProgressHUD dismiss];
                        
                        //
                        //
                    }else if ([[obj objectForKey:@"code"] intValue] == 202) {
                        NSDictionary *s1=[obj objectForKey:@"data"];
                        NSString * user = [NSString stringWithFormat:@"%@",[s1 objectForKey:@"userId"]];
                        NSString * sta = [NSString stringWithFormat:@"%@",[s1 objectForKey:@"status"]];
                        
                        [[NSUserDefaults standardUserDefaults] setObject:user forKey:@"userId"];
                        [[NSUserDefaults standardUserDefaults] setObject:[s1 objectForKey:@"token"] forKey:@"user_token"];
                        [[NSUserDefaults standardUserDefaults] setObject:[s1 objectForKey:@"nickName"] forKey:@"nickName"];
                        [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"openId"];
                        [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"OTC_openId"];
                        if ([s1 objectForKey:@"portrait"]  == nil) {
                            
                            [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"portrait"];
                        }
                        [[NSUserDefaults standardUserDefaults] setObject:[s1 objectForKey:@"portrait"] forKey:@"portrait"];
                        [[NSUserDefaults standardUserDefaults] setObject:self.zhanghaoText.text forKey:@"phoneNumber"];
                        [[NSUserDefaults standardUserDefaults] setObject:sta forKey:@"status"];
                        [[NSUserDefaults standardUserDefaults] setObject:self.mimaText.text forKey:@"passWord"];
                        self.registerButton.userInteractionEnabled=YES;
                        [SVProgressHUD dismiss];
                        UIStoryboard * story = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                        
                        XMWJMmViewController * WJMmVC = [story instantiateViewControllerWithIdentifier:@"XMWJMmViewController"];
                        WJMmVC.ison = @"1";
                        if (self.zhanghaoText.text.length == 11) {
                            WJMmVC.phonedh = self.zhanghaoText.text;
                        }
                        [self.navigationController pushViewController:WJMmVC animated:YES];
                        
                    }else{
                        
                        [SVProgressHUD dismiss];
                        self.husd=[WKProgressHUD popMessage:[obj objectForKey:@"message"] inView:self.view duration:1.5 animated:YES];
                        self.registerButton.userInteractionEnabled=YES;
                    }
                }
            }];
            //
        }
        
        
    }else if (![self.zhanghaoText.text isEqualToString:@""] && [self.mimaText.text isEqualToString:@""]) {
        
        if ([self.mimaText.placeholder isEqualToString:@"请输入密码"] )
        {
            
             self.husd=[WKProgressHUD popMessage:@"请输入账号密码" inView:self.view duration:1.5 animated:YES];
        }
        
        else
            
        {
            
             self.husd=[WKProgressHUD popMessage:@"请输入验证码" inView:self.view duration:1.5 animated:YES];
            
        }
        
        
    }else {
        
        self.husd=[WKProgressHUD popMessage:@"请输入账号" inView:self.view duration:1.5 animated:YES];
        
        
    }
    
    
    
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    if (textField == self.zhanghaoText ) {
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        else if (self.zhanghaoText.text.length >= 11) {
            self.zhanghaoText.text = [textField.text substringToIndex:11];
            
            return NO;
        }
        //设置密文切换时保持之前的输入内容
        NSString *allstr=[textField.text stringByReplacingCharactersInRange:range withString:string];
        if (self.zhanghaoText.isSecureTextEntry ==YES)
        {
            self.zhanghaoText.text=allstr;
            return NO;
        }
    }
    
    if (textField == self.mimaText ) {
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        else if (self.mimaText.text.length >= 15) {
            self.mimaText.text = [textField.text substringToIndex:15];
            
            return NO;
        }
        //设置密文切换时保持之前的输入内容
        NSString *allstr=[textField.text stringByReplacingCharactersInRange:range withString:string];
        if (self.mimaText.isSecureTextEntry ==YES)
        {
            self.mimaText.text=allstr;
            return NO;
        }
    }
    
        return YES;
    }

//注册
- (void)zhuceButtonAction:(UIButton *)sender {
    
    UIStoryboard * story = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    XMZhuCeViewController * zhuCeVC = [story instantiateViewControllerWithIdentifier:@"XMZhuCeViewController"];
    if (self.zhanghaoText.text.length == 11) {
       zhuCeVC.phonedh = self.zhanghaoText.text;
    }
    [self.navigationController pushViewController:zhuCeVC animated:YES];
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
